package com.yiyibook.controller;
import java.util.ArrayList;
import java.util.Date;

import com.yiyibook.controller.vo.OutOrderVO;
import com.yiyibook.entity.Book;
import com.yiyibook.entity.OrderHead;
import com.yiyibook.entity.OutWarehouseOrderHead;
import com.yiyibook.entity.OutWarehouseOrderLine;
import com.yiyibook.service.BookService;
import com.yiyibook.service.OrderHeadService;
import com.yiyibook.service.OutOrderHeadService;
import com.yiyibook.service.OutOrderLineService;
import com.yiyibook.util.Msg;
import com.yiyibook.util.OutWarehouseOrderHeadNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 〈功能简述〉
 *
 * @author xun.gong@hand-china.com
 * @date 2019/10/22
 */
@RestController
@RequestMapping("outOrderHeader")
public class OutOrderHeadController {

    @Autowired
    private OutOrderHeadService outOrderHeadService;

    @Autowired
    private OutOrderLineService outOrderLineService;

    @Autowired
    private OrderHeadService orderHeadService;

    @Autowired
    private BookService bookService;

    @PostMapping("/insertOutOrder")
    public Msg insertOutOrder(List<Integer> orderHeadIds,Integer memberId){
        if(orderHeadIds.size() > 0){
            List<String> list = new ArrayList<>();
            List<Integer> integers = new ArrayList<>();
            for(Integer orderHeadId : orderHeadIds){
                // 查看订单中书籍的库存是否充足
                List<OutOrderVO> outOrderVOS = orderHeadService.selectByHeadId(orderHeadId);
                if(outOrderVOS.size() > 0){
                    for(OutOrderVO outOrderVO : outOrderVOS){
                        if(outOrderVO.getStock() >= outOrderVO.getBookBuyQuantity()){
                            // 库存充足，可以出库
                            // 修改库存信息
                            Book book = bookService.selectById(outOrderVO.getBookId());
                            book.setStock(book.getStock()-outOrderVO.getBookBuyQuantity());
                            bookService.updateByCondition(book);
                            integers.add(orderHeadId);
                        }
                        list.add(outOrderVO.getBookTitle());
                    }
                }
            }
            // 新增出库信息
            OutWarehouseOrderHead outWarehouseOrderHead = new OutWarehouseOrderHead();
            // 获取数据库中最后一出库单的流水号
            OutWarehouseOrderHead ow1 = outOrderHeadService.selectLastNum();
            // 给出库单添加流水号
            if(ow1 == null){
                outWarehouseOrderHead.setOutWarehouseHeadNum(OutWarehouseOrderHeadNumber.setOutWarehouseOrderHeadNumber(null));
            }else {
                outWarehouseOrderHead.setOutWarehouseHeadNum(OutWarehouseOrderHeadNumber.setOutWarehouseOrderHeadNumber(ow1.getOutWarehouseHeadNum()));
            }
            outWarehouseOrderHead.setOutWarehouseDate(new Date());
            outWarehouseOrderHead.setOutWarehouseAdminId(memberId);
            OutWarehouseOrderHead insert = outOrderHeadService.insert(outWarehouseOrderHead);
            for(Integer orderHeadId : integers){
                // 修改订单状态
                OrderHead orderHead = orderHeadService.selectById(orderHeadId);
                orderHead.setOrderStatus("DELIVERED");
                orderHeadService.updateByCondition(orderHead);
                OutWarehouseOrderLine outWarehouseOrderLine = new OutWarehouseOrderLine();
                outWarehouseOrderLine.setOutWarehouseHeadId(insert.getOutWarehouseHeadId());
                outWarehouseOrderLine.setOrderHeadId(orderHeadId);
                outOrderLineService.insert(outWarehouseOrderLine);
            }
            return Msg.success().add("list",list);
        }
        return Msg.fail().add("errMsg","网络波动，请重试！");
    }
}
